package com.yk.anima.servlet;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.yk.anima.dao.ShoppingCartMapper;
import com.yk.anima.pojo.*;
import com.yk.anima.response.CommonCode;
import com.yk.anima.response.QueryResponseResult;
import com.yk.anima.response.QueryResult;
import com.yk.anima.response.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author xiaokai
 * @create 2021-05-15 09:56:04
 * @desc...对购物的业务的管理
 */

@Service
public class ShoppingCarServlet {

    @Autowired
    ShoppingCartMapper shoppingCartMapper;

    @Autowired
    ProductsCatServlet productsCatServlet;

    @Autowired
    ProductsDogServlet productsDogServlet;

    @Autowired
    ProductsHheepServlet productsHheepServlet;

    @Autowired
    ProductsDirdsServlet productsDirdsServlet;

    public ResponseResult save(int aId, int animalId, int id) {
        if (animalId == 1) {
            thisCat(aId, animalId, id);
        } else if (animalId == 2) {
            thisDirds(aId, animalId, id);
        } else if (animalId == 3) {
            thisDog(aId, animalId, id);
        } else if (animalId == 4) {
            thisHheep(aId, animalId, id);
        } else {
            return new ResponseResult(CommonCode.FAIL);

        }
        return new ResponseResult(CommonCode.SUCCESS);


    }

    public ResponseResult thisCat(int aId, int animalId, int id) {

        ProductsCat cat = productsCatServlet.findById(id);
        if (cat == null) {
            return new ResponseResult(CommonCode.FAIL);
        }

        ShoppingCart shoppingCar = new ShoppingCart();
        //描述
        shoppingCar.setDescription(cat.getDescription());
        //名字
        shoppingCar.setName(cat.getName());
        //性别;
        shoppingCar.setGender(cat.getGender());
        //价钱
        shoppingCar.setPrice(cat.getPrice());
        //宠物归属类Id
        shoppingCar.setAnimalId(animalId);
        //用ID
        String catPrice = cat.getPrice();
        shoppingCar.setCartAdminId(aId);
        String name = shoppingCar.getName();
        String price = shoppingCar.getPrice();
        Integer cartAdminId = shoppingCar.getCartAdminId();
        Integer animalid = shoppingCar.getAnimalId();
        String gender = shoppingCar.getGender();
//        ShoppingCart shoppingCart = shoppingCartMapper.findByNum(name, price, cartAdminId, animalid, gender);
        ShoppingCart shoppingCart = shoppingCartMapper.findByNum(name,cartAdminId, animalid, gender);
        if (shoppingCart != null) {
            Integer num = shoppingCart.getNum();
            num += 1;
            //当前宠物的价钱
            String money = shoppingCart.getPrice();
            //改变后的价钱
            int addMoney = Integer.parseInt(money) + Integer.parseInt(catPrice);
            //类型转换
            String cP = String.valueOf(addMoney);
            Integer getsId = shoppingCart.getsId();
            int number = shoppingCartMapper.updateByNumAndPrice(getsId, num, cP);
            return new ResponseResult(CommonCode.SUCCESS);
        }

        shoppingCar.setNum(1);
        shoppingCar.setsId(null);
        int number = shoppingCartMapper.insertSelective(shoppingCar);
        if (number > 0) {
            return new ResponseResult(CommonCode.SUCCESS);
        }
        return new ResponseResult(CommonCode.FAIL);
    }

    //ProductsDirds
    public ResponseResult thisDirds(int aId, int animalId, int id) {

        ProductsDirds dirds = productsDirdsServlet.findById(id);

        if (dirds == null) {
            return new ResponseResult(CommonCode.FAIL);
        }
        ShoppingCart shoppingCar = new ShoppingCart();
        //描述
        shoppingCar.setDescription(dirds.getDescription());
        //名字
        shoppingCar.setName(dirds.getName());
        //性别;
        shoppingCar.setGender(dirds.getGender());
        //价钱
        shoppingCar.setPrice(dirds.getPrice());
        //宠物归属类Id
        shoppingCar.setAnimalId(animalId);
        //用ID
        shoppingCar.setCartAdminId(aId);
        //鸟的价钱
        String dirdsPrice = dirds.getPrice();
        String name = shoppingCar.getName();
        String price = shoppingCar.getPrice();
        Integer cartAdminId = shoppingCar.getCartAdminId();
        Integer animalid = shoppingCar.getAnimalId();
        String gender = shoppingCar.getGender();
        ShoppingCart shoppingCart = shoppingCartMapper.findByNum(name,cartAdminId, animalid, gender);
        if (shoppingCart != null) {
            Integer num = shoppingCart.getNum();
            num += 1;
            //当前宠物的价钱
            String money = shoppingCart.getPrice();
            int addMoney = Integer.parseInt(money) + Integer.parseInt(dirdsPrice);
            //改变后的价钱
            String cP = String.valueOf(addMoney);
            Integer getsId = shoppingCart.getsId();
            int number = shoppingCartMapper.updateByNumAndPrice(getsId, num, cP);
            return new ResponseResult(CommonCode.SUCCESS);
        }

        shoppingCar.setNum(1);
        shoppingCar.setsId(null);
        int number = shoppingCartMapper.insertSelective(shoppingCar);
        if (number > 0) {
            return new ResponseResult(CommonCode.SUCCESS);
        }
        return new ResponseResult(CommonCode.FAIL);
    }

    //ProductsDog
    public ResponseResult thisDog(int aId, int animalId, int id) {

        ProductsDog dog = productsDogServlet.findById(id);

        if (dog == null) {
            return new ResponseResult(CommonCode.FAIL);
        }
        ShoppingCart shoppingCar = new ShoppingCart();
        //描述
        shoppingCar.setDescription(dog.getDescription());
        //名字
        shoppingCar.setName(dog.getName());
        //性别;
        shoppingCar.setGender(dog.getGender());
        //价钱
        shoppingCar.setPrice(dog.getPrice());
        //宠物归属类Id
        shoppingCar.setAnimalId(animalId);
        //用ID
        shoppingCar.setCartAdminId(aId);
        String dogPrice = dog.getPrice();
        String name = shoppingCar.getName();
        String price = shoppingCar.getPrice();
        Integer cartAdminId = shoppingCar.getCartAdminId();
        Integer animalid = shoppingCar.getAnimalId();
        String gender = shoppingCar.getGender();
        ShoppingCart shoppingCart = shoppingCartMapper.findByNum(name,cartAdminId, animalid, gender);
        if (shoppingCart != null) {
            Integer num = shoppingCart.getNum();
            num += 1;
            //当前宠物的价钱
            String money = shoppingCart.getPrice();
            int addMoney = Integer.parseInt(money) + Integer.parseInt(dogPrice);
            //改变后的价钱
            String cP = String.valueOf(addMoney);
            Integer getsId = shoppingCart.getsId();
            shoppingCartMapper.updateByNumAndPrice(getsId, num, cP);
            return new ResponseResult(CommonCode.SUCCESS);
        }

        shoppingCar.setNum(1);
        shoppingCar.setsId(null);
        int number = shoppingCartMapper.insertSelective(shoppingCar);
        if (number > 0) {
            return new ResponseResult(CommonCode.SUCCESS);
        }
        return new ResponseResult(CommonCode.FAIL);
    }

    //ProductsHheep
    public ResponseResult thisHheep(int aId, int animalId, int id) {

        ProductsHheep heep = productsHheepServlet.findById(id);

        if (heep == null) {
            return new ResponseResult(CommonCode.FAIL);
        }
        ShoppingCart shoppingCar = new ShoppingCart();
        //描述
        shoppingCar.setDescription(heep.getDescription());
        //名字
        shoppingCar.setName(heep.getName());
        //性别;
        shoppingCar.setGender(heep.getGender());
        //价钱
        shoppingCar.setPrice(heep.getPrice());
        //宠物归属类Id
        shoppingCar.setAnimalId(animalId);
        //用ID
        shoppingCar.setCartAdminId(aId);
        String heepPrice = heep.getPrice();
        String name = shoppingCar.getName();
        String price = shoppingCar.getPrice();
        Integer cartAdminId = shoppingCar.getCartAdminId();
        Integer animalid = shoppingCar.getAnimalId();
        String gender = shoppingCar.getGender();
        ShoppingCart shoppingCart = shoppingCartMapper.findByNum(name,cartAdminId, animalid, gender);
        if (shoppingCart != null) {
            Integer num = shoppingCart.getNum();
            num += 1;
            //当前宠物的价钱
            String money = shoppingCart.getPrice();
            //改变后的价钱
            String cP = String.valueOf(Integer.parseInt(money) + Integer.parseInt(heepPrice));
            Integer getsId = shoppingCart.getsId();
            shoppingCartMapper.updateByNumAndPrice(getsId, num, cP);
            return new ResponseResult(CommonCode.SUCCESS);
        }

        shoppingCar.setNum(1);
        shoppingCar.setsId(null);
        int number = shoppingCartMapper.insertSelective(shoppingCar);
        if (number > 0) {
            return new ResponseResult(CommonCode.SUCCESS);
        }
        return new ResponseResult(CommonCode.FAIL);
    }

    public List<ShoppingCart> findById(int cartAdminId) {
        List<ShoppingCart> list = shoppingCartMapper.findById(cartAdminId);
        if (list.size() == 0) {
            return null;
        }
        return list;
    }


    public ResponseResult deleteById(int cartAdminId, int sId) {

        int number = shoppingCartMapper.deleteById(cartAdminId, sId);
        if (number > 0) {
            return new ResponseResult(CommonCode.SUCCESS);
        }
        return new ResponseResult(CommonCode.FAIL);
    }

    public ResponseResult clear(int cartAdminId) {
        int number = shoppingCartMapper.clear(cartAdminId);
        if (number > 0) {
            return new ResponseResult(CommonCode.SUCCESS);
        }
        return new ResponseResult(CommonCode.FAIL);
    }

    public ResponseResult updateByNumAdd(int cartAdminId, int sId, int animalId, int id) {
       // String price;
//        if (animalId == 1) {
//            ProductsCat productsCat = productsCatServlet.findById(id);
//            if (productsCat == null) {
//                return new ResponseResult(CommonCode.FAIL);
//            }
//            price = productsCat.getPrice();
//        } else if (animalId == 2) {
//            ProductsDirds productsDirds = productsDirdsServlet.findById(id);
//            if (productsDirds == null) {
//                return new ResponseResult(CommonCode.FAIL);
//            }
//            price = productsDirds.getPrice();
//        } else if (animalId == 3) {
//            ProductsDog productsDog = productsDogServlet.findById(id);
//            if (productsDog == null) {
//                return new ResponseResult(CommonCode.FAIL);
//            }
//            price = productsDog.getPrice();
//        } else if (animalId == 4) {
//            ProductsHheep productsHheep = productsHheepServlet.findById(id);
//            if (productsHheep == null) {
//                return new ResponseResult(CommonCode.FAIL);
//            }
//            price = productsHheep.getPrice();
//        } else {
//            return new ResponseResult(CommonCode.FAIL);
//        }
        String price = addAndLess(animalId, id);

        ShoppingCart shoppingCart = shoppingCartMapper.findByCartAdminIdAndSId(cartAdminId, sId);
        if (shoppingCart == null) {
            return new ResponseResult(CommonCode.FAIL);
        }
        String shoppingCartPrice = shoppingCart.getPrice();
        int i = Integer.parseInt(shoppingCartPrice) + Integer.parseInt(price);
        String addMoney = String.valueOf(i);
        Integer num = shoppingCart.getNum();
        num++;

        int number = shoppingCartMapper.updateByNumAndPrice(sId, num, addMoney);
        if (number > 0) {
            return new ResponseResult(CommonCode.SUCCESS);
        }
        return new ResponseResult(CommonCode.FAIL);

    }

    public ResponseResult updateByNumLess(int cartAdminId, int sId, int animalId, int id) {
        String price;
        if (animalId == 1) {
            ProductsCat productsCat = productsCatServlet.findById(id);
            if (productsCat == null) {
                return new ResponseResult(CommonCode.FAIL);
            }
            price = productsCat.getPrice();
        } else if (animalId == 2) {
            ProductsDirds productsDirds = productsDirdsServlet.findById(id);
            if (productsDirds == null) {
                return new ResponseResult(CommonCode.FAIL);
            }
            price = productsDirds.getPrice();
        } else if (animalId == 3) {
            ProductsDog productsDog = productsDogServlet.findById(id);
            if (productsDog == null) {
                return new ResponseResult(CommonCode.FAIL);
            }
            price = productsDog.getPrice();
        } else if (animalId == 4) {
            ProductsHheep productsHheep = productsHheepServlet.findById(id);
            if (productsHheep == null) {
                return new ResponseResult(CommonCode.FAIL);
            }
            price = productsHheep.getPrice();
        } else {
            return new ResponseResult(CommonCode.FAIL);
        }
        ShoppingCart shoppingCart = shoppingCartMapper.findByCartAdminIdAndSId(cartAdminId, sId);
        if (shoppingCart == null) {
            return new ResponseResult(CommonCode.FAIL);
        }
        Integer num = shoppingCart.getNum();
        if (num==1){
            this.deleteById(cartAdminId,sId);
            return new ResponseResult(CommonCode.FAIL);
        }
        if (num <= 0) {
            return new ResponseResult(CommonCode.FAIL);
        }
        String shoppingCartPrice = shoppingCart.getPrice();

        String addMoney = String.valueOf(Integer.parseInt(shoppingCartPrice) - Integer.parseInt(price));
        num--;
        int number = shoppingCartMapper.updateByNumAndPrice(sId, num,addMoney);
        if (number > 0) {
            return new ResponseResult(CommonCode.SUCCESS);
        }
        return new ResponseResult(CommonCode.FAIL);
    }

    public String addAndLess(int animalId, int id) {
        String price = null;
        if (animalId == 1) {
            ProductsCat productsCat = productsCatServlet.findById(id);
            price = productsCat.getPrice();
        } else if (animalId == 2) {
            ProductsDirds productsDirds = productsDirdsServlet.findById(id);
            price = productsDirds.getPrice();
        } else if (animalId == 3) {
            ProductsDog productsDog = productsDogServlet.findById(id);
            price = productsDog.getPrice();
        } else if (animalId == 4) {
            ProductsHheep productsHheep = productsHheepServlet.findById(id);
            price = productsHheep.getPrice();
        }
        return price;

    }

    public QueryResponseResult findByPage(int page, int size, String name) {
        if (page<=0){
            page=1;
        }
        if (size<0){
            size=2;
        }

        PageHelper.startPage(page,size);
        Page<ShoppingCart> cartPage=shoppingCartMapper.findByName(name);
        long total = cartPage.getTotal();
        List<ShoppingCart> result = cartPage.getResult();
        QueryResult<ShoppingCart> queryResult = new QueryResult<>();
        queryResult.setTotal(total);
       queryResult.setList(result);
        return new QueryResponseResult(CommonCode.SUCCESS,queryResult);

    }
}
